package icu.groot.alan.hotel.util.log;

import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * AOP切入点记录请求响应数据
 */
@Aspect
@Order(5)
@Component
@Slf4j
public class ControllerInterceptor {

    /**
     * 切入点表达式
     */
    @Pointcut("execution(* icu.groot.alan.hotel.controller.*.*.*(..))")
    public void controllerLog() {
    }

    /**
     * 进入controller之前，拿请求参数
     */
    @Before("controllerLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {

    }

    /**
     * 接口执行之后，获取响应结果
     */
    @AfterReturning(returning = "controllerResult", pointcut = "controllerLog()")
    public void doAfterReturning(Object controllerResult) throws Throwable {
        log.info("controllerResult : \r\n{}", new GsonBuilder().setPrettyPrinting().create().toJson(controllerResult));
    }
}